﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Security.Principal;
using System.Net;
using System.Configuration;
using Crosland.Automotriz.Facade.Interface;
using Crosland.Automotriz.Facade.Facade;
using Crosland.Automotriz.Core;
using Crosland.Automotriz.Logic.Linq;

namespace Crosland.Automotriz.Web.Repuestos
{
    public partial class wFrmSituacionArticuloReport : System.Web.UI.Page
    {
        iFacadeSistema cls_man_modelo_imagenlogic = new FacadeSistema();
        string idNegocio = ConfigurationManager.AppSettings["Negocio"];
        protected void Page_Load(object sender, EventArgs e)
        {

            if(!IsPostBack){

                lbl_Cod_Material.Text = Session["CODIGO_MATERIAL"].ToString();

                this.mostrarReport();
            
            
            }


        }


        void mostrarReport()
        {

            try
            {
                int id_material = Convert.ToInt32(Session["ID_MATERIAL"].ToString());
               
                ReportViewer1.ServerReport.ReportServerCredentials = new MyReportServerCredentials();
                ReportViewer1.ProcessingMode = ProcessingMode.Remote;
                ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["RsServer"]);
                ReportViewer1.ServerReport.ReportPath = "/SAP_Repuestos/rsSituacionArticulo";


                ReportParameter[] parametro = new ReportParameter[1];
                parametro[0] = new ReportParameter();
                parametro[0].Name = "id_material_princ";
                parametro[0].Values.Add(Convert.ToString(id_material));
                parametro[0].Visible = false;

                ReportViewer1.ServerReport.SetParameters(parametro);
                ReportViewer1.ServerReport.Refresh();

            }
            catch (Exception ex)
            {
                lblmensaje.Text = ex.Message;
                string scriptAprobado = "ShowpopupMsj();";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "carga", scriptAprobado, true);


            }




            



        }

        //void validarSessiones()
        //{
        //    Session["sistema"] = Convert.ToInt32(Request.QueryString["idSistema"]);
        //    Session["modelo"] = Convert.ToInt32(Request.QueryString["idModelo"]);

        //}

        //void CargarModeloSistema()
        //{
        //    try
        //    {
        //        clsT_Man_SistemaLogic t_man_sistemaBL = new clsT_Man_SistemaLogic();

        //        List<T_MAN_SISTEMA> listaSistema = new List<T_MAN_SISTEMA>();
        //        var q = (from c in t_man_sistemaBL.listarSistema()
        //                 where c.ID_SISTEMA == Convert.ToInt32(Request.QueryString["idSistema"])
        //                 select c).Take(1);
        //        listaSistema = q.ToList();

        //        clsTd_ModeloLogic t_man_ModeloBL = new clsTd_ModeloLogic();

        //        List<TD_MODELO> listaModelo = new List<TD_MODELO>();
        //        var m = (from c in t_man_ModeloBL.listar_filtro_S()
        //                 where c.ID_MODELO == Convert.ToInt32(Request.QueryString["idModelo"])
        //                 select c).Take(1);

        //        listaModelo = m.ToList();


        //        lblModelo0.Text = (listaModelo[0].DES_MODELO).ToString();
        //        lbldes_sistema.Text = (listaSistema[0].DES_SISTEMA).ToString();
        //        lbldes_sistema0.Text = (listaSistema[0].DES_SISTEMA_INGLES).ToString();

        //        Session["DES_SISTEMA"] = (listaSistema[0].DES_SISTEMA_INGLES).ToString();
        //    }
        //    catch
        //    {

        //    }
        //}

        //void listarArticulo()
        //{

        //    try
        //    {

        //        clsT_Man_Sistema_ModeloLogic t_man_sistema_modeloBL = new clsT_Man_Sistema_ModeloLogic();
        //        List<T_MAN_SISTEMA_MODELO> lista = new List<T_MAN_SISTEMA_MODELO>();
        //        var q = (from c in t_man_sistema_modeloBL.lista()
        //                 where c.ID_MODELO == Convert.ToInt32(Request.QueryString["idModelo"]) && c.ID_SISTEMA == Convert.ToInt32(Request.QueryString["idSistema"])
        //                 select c).Take(1);

        //        lista = q.ToList();

        //        imagen.ImageUrl = ConfigurationManager.AppSettings["RutaFotoSistema"] + "/" + (lista[0].DES_IMAGEN).ToString();

        //        gvPopDetalleArticulos.DataSource = cls_man_modelo_imagenlogic.listar_sistema_articulo(Convert.ToInt32(Request.QueryString["idSistema"]), Convert.ToInt32(Request.QueryString["idModelo"]));
        //        gvPopDetalleArticulos.DataBind();
        //        int nro_pagina = Convert.ToInt32(gvPopDetalleArticulos.GetRowValues(gvPopDetalleArticulos.FocusedRowIndex, "NRO_PAGINA"));
        //        lblnropagina.Text = nro_pagina.ToString();

        //        Session["DES_URL"] = ConfigurationManager.AppSettings["RutaFotoSistema"] + "/" + (lista[0].DES_IMAGEN).ToString();
        //    }
        //    catch
        //    {


        //    }
        //}


        



        [Serializable]
        public sealed class MyReportServerCredentials : IReportServerCredentials
        {
            public WindowsIdentity ImpersonationUser
            {
                get
                {

                    return null;
                }
            }

            public ICredentials NetworkCredentials
            {
                get
                {


                    // User name
                    string userName = ConfigurationManager.AppSettings["RSUser"];

                    if (string.IsNullOrEmpty(userName))
                        throw new Exception(
                            "Missing user name from web.config file");

                    // Password
                    string password =
                        ConfigurationManager.AppSettings["RSPassword"];

                    if (string.IsNullOrEmpty(password))
                        throw new Exception(
                            "Missing password from web.config file");

                    // Domain
                    string domain =
                        ConfigurationManager.AppSettings["RSDomain"];

                    if (string.IsNullOrEmpty(domain))
                        throw new Exception(
                            "Missing domain from web.config file");

                    return new NetworkCredential(userName, password, domain);
                }
            }

            public bool GetFormsCredentials(out Cookie authCookie,
                        out string userName, out string password,
                        out string authority)
            {
                authCookie = null;
                userName = null;
                password = null;
                authority = null;


                return false;
            }


        }

        protected void gvPopDetalleArticulos_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {

        }

        protected void btnAtras_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Repuestos/wFrmPronosticoImportacionActualizar.aspx?id=3");
        }



    }
}